package com.hdcx.Minfresh.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.BaseActivity;
import com.hdcx.Minfresh.activity.CollectActivity;
import com.hdcx.Minfresh.bean.CollectWrapper;
import com.hdcx.Minfresh.util.AppUtil;

/**
 * Created by Administrator on 2016/11/9.
 */

public class CollectAdapter extends RecyclerView.Adapter<CollectAdapter.Holder> {
    BaseActivity mActivity;
    CollectWrapper data;
    int tag =0;

    public CollectAdapter(BaseActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        view = layoutInflater.inflate(R.layout.item_activity_collect, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final CollectWrapper.ListBean_Collect temp = data.getList().get(position);

        AppUtil.showPic(temp.getPic(),holder.img_main);
        holder.tv_name.setText(temp.getTitle());
        holder.tv_intro.setText(temp.getShort_title());
        holder.tv_intro_2.setText("规格:" + temp.getEtalon() + temp.getUnits());
        holder.tv_money.setText(temp.getPrice() + temp.getUnits());
        holder.tv_units.setText("1"+temp.getUnits());
        holder.tv_delect.setVisibility(View.VISIBLE);
        if(tag==1){
            holder.tv_delect.setText(temp.getLast_time());
          holder.layout_all.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  mActivity.onMyItemClick(temp.getId());
              }
          });
        }else{
            holder.tv_delect.setText("取消收藏");
            holder.tv_delect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.onMyItemClick(temp.getId());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
       if(data==null || data.getList()==null){
           return 0;
       }
        return data.getList().size();
    }

    public void setData(CollectWrapper response) {
        data = response;
        notifyDataSetChanged();
    }

    public void setData(CollectWrapper response, int i) {
        data = response;
        tag= i;
        notifyDataSetChanged();
    }

    public class Holder extends RecyclerView.ViewHolder {
        public TextView tv_intro, tv_intro_2, tv_units, tv_name, tv_money ,tv_delect;
        ImageView img_main;
        LinearLayout layout_all;

        public Holder(View itemView) {
            super(itemView);
            tv_intro = (TextView) itemView.findViewById(R.id.tv_intro);
            tv_intro_2 = (TextView) itemView.findViewById(R.id.tv_intro_2);
            tv_units = (TextView) itemView.findViewById(R.id.tv_units);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_money = (TextView) itemView.findViewById(R.id.tv_money);
            tv_delect = (TextView) itemView.findViewById(R.id.tv_delect);
            img_main = (ImageView) itemView.findViewById(R.id.img_main);
            layout_all = (LinearLayout) itemView.findViewById(R.id.layout_all);
        }
    }
}
